<template>
	<view class="listPage">

		<view class="listInput">
			<view class="search-card">
				<view @click="handFocus" style="line-height: 76upx;height: 76upx;">{{treeName}}</view>
<!-- 				<input style="width: 100%;height: 100%;" type="text" @focus='handFocus' v-model="treeName"
					placeholder="根据标段查询" /> -->
			</view>
			
	
<tree ref="company" nodeKey="fCompanyid" rangeKey="fShortname" :selectParent="selectParentTree"
			:multiple="multipleTree" :range="companyTree" :foldAll="flod" @confirm="treeConfirm" @cancel="treeCancel">
		</tree>
		</view>
	
		

		<view class="listList" >
			<tki-tree ref="tkitree" :selectParent="selectParent" :multiple="multiple" :range="list" :foldAll="flod"
				rangeKey="fEntrynumber" isShowS="isShows" names="fMaterialname" classs="fSpecification"
				fCement="fManufacturername" fSand="fBatchnumber" fStone2="fEntrynum" fStone3="fUnit"
				fWater="fEntrydates" fId="fId" :selectId="selectId" @confirm="confirm" @cancel="cancel">
			</tki-tree>
		</view>


	</view>
</template>

<script>
	import tkiTree from '@/components/treeData/treeList/tki-tree.vue';
	// import xfxSsearch from '@/components/xfx-ssearch/index'
	import tree from '@/components/tki-tree/tki-tree';
	export default {
		data() {
			return {
				optionData:null,//上个页面的传参
				// 树结构
				companyTree: [],
				treeName: "",
				selectParentTree: true,
				multipleTree: false,
				flod: true,
				// 树结构
				// 选择单位
				list: [],
				multiple: false,
				selectParent: true,
				// 用户信息
				userInfo: {},
				fBusinessscopecompanyid: "",
				selectId:null,
				dataList:[]
				// MaterialTypeId: ""
			}
		},
		components: {
			tkiTree,
			// xfxSsearch,
			tree
		},
		onLoad(options) {
			// this.fBusinessscopecompanyid = options.editItem
			// this.getLocalData();
			var that = this;
			that.optionData=options;
			this.selectId=options.selectId
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = JSON.parse(res.data)
					that.treeList();
				}
			})
		},
		mounted() {},
		methods: {
			handFocus() {
				this.$refs.company._show();
			},
			treeList() {
				this.$http.getUserCompanyTreeList({
						companyId: this.userInfo.companyId,
						userId: this.userInfo.userId,
					})
					.then((res) => {
						if (res.errcode == 0) {
							var treeData = this.tranListToTreeData(
								res.data.userCompanyTreeList
							);
							this.companyTree = treeData; //机构树
							// console.log(this.companyTree);
							if (treeData[0].children == undefined) {
								this.treeName = treeData[0].fShortname;
								this.treeConfirm(treeData);
								this.$refs.company._hide()
							} else {
								var that = this
								uni.getStorage({
									key: 'userInfo',
									success: function(res) {
										that.userInfo = JSON.parse(res.data)
										that.dataList.fCompanyid = that.userInfo.companyId;
										that.treeName =that.userInfo.companyName;
										that.getListData(that.dataList.fCompanyid);
									}
								})

							}

						} else {
							uni.showToast({
								title: "获取机构树失败",
								icon: 'none',
								duration: 2000
							});
							return false;
						}
					})

			},
			tranListToTreeData(data) {
				let result = [];
				if (!Array.isArray(data)) {
					return result;
				}
				// 清除已存在的children 重新赋值
				data.forEach((item) => {
					delete item.children;
				});

				let map = {};
				data.forEach((item) => {
					map[item.fCompanyid] = item;
				});

				data.forEach((item) => {
					let parent = map[item.fParentid];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},
			treeConfirm(e) {
				console.log('9999',e);
				this.treeName = e[0].fShortname;
				this.getListData(e[0].fCompanyid);
			},
			treeCancel() {},

			getListData(id) {
				var isShows = false
				var that = this;
				var data = {
					endDates: "",
					fCompanyid: id,
					keyType: "",
					modelType: 2,
					page: 1,
					pageSize: 20,
					startDates: "",
					taskNumber: "",
					userId: that.userInfo.userId,
				}

				this.$http.getEntryledgerPageLists(data).then(res => {
					if (res.errcode == 0) {
						res.data.pageInfo.list.map((item) => {
							item.isShows = false
						})
						that.list = res.data.pageInfo.list;
					} else {
						console.log("接口报错")
					}
				}, (err) => {
					uni.showToast({
						title: "获取数据失败",
						icon: 'none',
						duration: 2000
					});
				})

			},
			toggle: function(item) {
				console.log("toggle", item);
				item.show = !item.show;
			},
			// 取消回调事件
			cancel(e) {
				// console.log(e)
				uni.navigateBack({
					delta: 1
				})
			},
			// // 显示树形选择器
			// showTree() {
			// 	console.log("showTree");
			// 	this.$refs.tkitree._show();
			// },
			// 确定回调事件
			confirm(e) {
				var that = this
				if (e[0] != undefined) {
					var editItem = e[0]
					
					
					//// App
					let pages = getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.otherFun(editItem); //重点$vm  下个页面接数据函数
					uni.navigateBack();
					//// App
					

				} else {
					uni.showToast({
						title: "请选择进场材料",
						icon: 'none',
						duration: 2000
					})
				}
			},

		}
	}
</script>

<style lang="less" scoped>
	.listPage {
		width: 100%;
		height: 100%;
		background-color: #fff;
		overflow: hidden;

		.listInput {
			width: 100%;
			height: 100upx;

			.search-card {
				margin-top: 30upx;
				width: 100%;
				height: 100%;
				padding: 10upx;
				background: #E4E7ED;
				border: 1px solid #e2e2e2;
				border-radius: 40upx;

				input {
					width: 100%;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}

		.listList {
			width: 100%;
			height: calc(100% - 130upx);
			overflow-y: auto;
		}
	}
</style>
